﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 材料到发货管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"SELECT t1.ID
,t1.单据ID
,t1.材料ID
,t2.品名
,t2.规格
,t2.单位
,case when t1.发货=1 then
	t5.名称
else
    t3.名称
end as 供货或使用
,t1.日期
,cast(发货 as tinyint) as 到发
,cast(t1.单价 as float) as 单价
,cast(abs(t1.数量) as float) as 数量
,t1.金额
,t1.保管员
,t4.单据号
FROM 材料到发货 t1
left outer join 物料品种 t2 on t2.ID=t1.材料ID
left outer join 材料供货单位 t3 on t3.ID=t1.单位ID
left outer join 材料入出库 t4 on t4.ID=t1.单据ID
left outer join 单位编码 t5 on t5.ID=t1.单位ID";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label_Result.Text = "";
            if (!this.IsPostBack)
            {
                string s = Request.QueryString["fid"];
                if (s == null)
                {
                    s = "";
                }
                int fid = 0;
                int.TryParse(s, out fid);
                this.Label_FID.Text = fid.ToString("D7");

                this.SqlText.Text = sql;
                if (fid == 0)
                {
                }
                else if (fid % 100000 == 0)
                {
                    this.SqlText.Text += " where t1.材料ID>=" + (fid * 1000) + " and t1.材料ID<" + ((fid + 100000) * 1000);
                }
                else if (fid % 1000 == 0)
                {
                    this.SqlText.Text += " where t1.材料ID>=" + (fid * 1000) + " and t1.材料ID<" + ((fid + 1000) * 1000);
                }
                else
                {
                    this.SqlText.Text += " where t1.材料ID>=" + (fid * 1000) + " and t1.材料ID<" + ((fid + 1) * 1000);
                }
                this.SqlText.Text += " order by id desc";
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;

            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.Button_Out.OnClientClick = "parent.swin2_load('/Store/材料发货记录录入.aspx',600,340);";

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            if ((int)this.GridView1.SelectedDataKey[2] == 0)
            {
                this.Button_Delete.Enabled = true;
            }

            this.Button_Edit.Enabled = true;
            if ((byte)this.GridView1.SelectedDataKey[1] == 0)
            {
                this.Button_Edit.OnClientClick = "parent.swin2_load('/Store/材料到货记录录入.aspx?id=" 
                    + this.Button_Delete.CommandName + "',600,340);return false;";
            }
            else
            {
                this.Button_Edit.OnClientClick = "parent.swin2_load('/Store/材料发货记录录入.aspx?id="
                    + this.Button_Delete.CommandName + "',600,340);return false;";
            }
            this.Button_Out.OnClientClick = "parent.swin2_load('/Store/材料发货记录录入.aspx?inid=" + (int)this.GridView1.SelectedDataKey[0] + "',600,340);";


            if (this.Button_Bill.CommandArgument == "," || this.Button_Bill.CommandArgument == "")
            {
                if ((int)this.GridView1.SelectedDataKey[2] == 0)
                {
                    //this.Button_Bill.Visible = true;
                    this.Button_Bill.Enabled = true;
                    if ((byte)this.GridView1.SelectedDataKey[1] == 0)
                    {
                        this.Button_Bill.Text = "填写入库单";
                        this.Button_Bill.OnClientClick = "parent.swin2_load('/Store/材料入库单录入.aspx?list="
                            + (int)this.GridView1.SelectedDataKey[0] + "',710,410);";
                    }
                    else
                    {

                        this.Button_Bill.Text = "填写出库单";
                        this.Button_Bill.OnClientClick = "parent.swin2_load('/Store/材料出库单录入.aspx?list="
                            + (int)this.GridView1.SelectedDataKey[0] + "',710,410);";
                    }
                }
                else
                {
                    //this.Button_Bill.Visible = false;
                    this.Button_Bill.Enabled = false;
                }
            }

        }
        private string GetState()
        {
            string state = "";
            int bm=0;
            if(int.TryParse(this.BianMa.Text,out bm))
            {
                if (bm > 0)
                {
                    state += " and t1.材料ID=" + bm;
                }
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }
            if (this.GongHuo.Text != "0" && this.GongHuo.Text != "" && this.FenChang.Text!="350000")
            {
                state += " and (t1.发货=0 and t1.单位ID=" + this.GongHuo.Text
                    + " or t1.发货=1 and t1.单位ID=" + this.FenChang.Text + ")";
            }
            else if (this.GongHuo.Text != "0" && this.GongHuo.Text != "" && this.FenChang.Text == "350000")
            {
                state += " and (t1.发货=0 and t1.单位ID=" + this.GongHuo.Text
                    + " or t1.发货=1)";
            }
            else if (this.GongHuo.Text == "0" && this.FenChang.Text != "0")
            {
                state += " and (t1.发货=0 or t1.发货=1 and t1.单位ID=" + this.FenChang.Text + ")";
            }

            this.Button_Bill.Text = "入库出库";

            if (this.XianShi.Text == "1")
            {
                state += " and t1.发货=0";
            }
            else if (this.XianShi.Text == "2")
            {
                state += " and t1.发货=1";
            }
            else if (this.XianShi.Text == "3")
            {
                state += " and t1.发货=0 and t1.单据ID=0";
                if (bm>0 && this.GongHuo.Text != "0")
                {
                    this.GridView1.Columns[11].Visible = true;
                    this.Button_Bill.Text = "填写入库单";
                    //this.Button_Bill.Visible = true;
                }
            }
            else if (this.XianShi.Text == "4")
            {
                state += " and t1.发货=1 and t1.单据ID=0";
                if (bm > 0 && this.FenChang.Text != "0")
                {
                    this.GridView1.Columns[11].Visible = true;
                    this.Button_Bill.Text = "填写出库单";
                    //this.Button_Bill.Visible = true;
                }
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            //this.Button_Bill.Visible = false;
            this.Button_Bill.Enabled = false;
            this.Button_Bill.CommandArgument = ",";

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {

            this.BianMa.Text = "";
            this.PinMing.Text = "";
            this.GongHuo.Text = "0";
            this.FenChang.Text = "350000";
            this.XianShi.Text = "0";
            this.Date1.Text = "";
            this.Date2.Text = "";

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((byte)DataBinder.Eval(e.Row.DataItem, "到发") == 1)
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkbox = (CheckBox)sender;
            Label label = (Label)checkbox.Parent.FindControl("Label_rowid");
            if (checkbox.Checked)
            {
                this.Button_Bill.CommandArgument += label.Text + ",";
            }
            else
            {
                this.Button_Bill.CommandArgument = this.Button_Bill.CommandArgument.Replace("," + label.Text + ",", ",");
            }
            if (this.Button_Bill.CommandArgument == ",")
            {
                this.Button_Bill.Enabled = false;
            }
            else
            {
                this.Button_Bill.Enabled = true;
            }
            if (this.Button_Bill.Text == "填写出库单")
            {
                this.Button_Bill.OnClientClick = "parent.swin2_load('/Store/材料出库单录入.aspx?list="
                    + this.Button_Bill.CommandArgument.Substring(1) + "',710,410);";
            }
            else
            {
                this.Button_Bill.OnClientClick = "parent.swin2_load('/Store/材料入库单录入.aspx?list=" 
                    + this.Button_Bill.CommandArgument.Substring(1) + "',710,410);";
            }
        }

        protected void Button_Stat_Click(object sender, EventArgs e)
        {

            int fid1=0, fid2=2099999;
            fid1 = int.Parse(this.Label_FID.Text);
            if (fid1 == 0)
            {
            }
            else if (fid1 % 100000 == 0)
            {
                fid2 = fid1 + 99999;
            }
            else if (fid1 % 1000 == 0)
            {
                fid2 = fid1 + 999;
            }
            else
            {
                fid2 = fid1;
            }
            fid1 *= 1000;
            fid2 *= 1000;
            decimal dhsl = 0, fhsl = 0;
            decimal dhje = 0,fhje=0;
            int dhts = 0, fhts = 0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select 发货,isnull(sum(数量),0),isnull(sum(金额),0),count(*) from 材料到发货 where 材料ID between " + fid1 + " and " + fid2;

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                Cmd.CommandText += " and 日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                Cmd.CommandText += " and 日期<='" + d.ToShortDateString() + "'";
            }
            Cmd.CommandText += " group by 发货;";

            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();

            while (Reader.Read())
            {
                if ((bool)Reader[0])
                {
                    fhsl = (decimal)Reader[1];
                    fhje =(decimal)Reader[2];
                    fhts = (int)Reader[3];
                    
                }
                else
                {
                    dhsl = (decimal)Reader[1];
                    dhje = (decimal)Reader[2];
                    dhts = (int)Reader[3];
                }
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
            this.Label_Result.Text = "到货数量：" + dhsl + "(" + dhts + "条)" + " 到货金额：" + dhje + "元 发货数量：" + fhsl + "(" + fhts + "条)" + " 发货金额：" + fhje+"元";
        }
    }
}
